<?php
class Controller_ProjectUser extends Controller_Website {
    
    public function action_list()
    {
        $pro = Model::factory('project');
        $pro = $pro->getAll(array('type'=>0))->as_array();
//        print_r($pro);exit;
        foreach ($pro as &$p)
        {
//            print_r($p);exit;
            $pro_user = Model::factory('projectUser');
            $pro_user = $pro_user->getAll(array('project_id'=>$p['id'],'is_accept'=>1))->as_array(); //将该项目中已有的人员查出来
//            print_r($pro_user);exit;
            foreach ($pro_user as &$u)  //将项目中的人职业和姓名查出来
            {
                $zhiye = Model::factory('personCategory');
                $zhiye = $zhiye->getRow(array('id'=>$u['person_cate_id']));
//                print_r($u);exit;
                $username = Model::factory('personInfo');
                $username = $username->getRow(array('uid'=>$u['user_id']));
                $u['username'] = $username['real_name'];
                $u['name']  = $zhiye['name'];
            }
//            print_r($pro_user);exit;
            $p['users'] =$pro_user;
//            print_r($p);exit;
            $prop = Model::factory('projectProps');
            $prop = $prop->getRow(array('project_id'=>$p['id']));
            $arr = json_decode($prop['prop_middle'],true);
            $arr1 = json_decode($prop['zhizuo_prop_middle'],true);
            $name = 0;
//            print_r($arr);exit;
            $newArray = [];
            foreach ($arr as $k=>$v)  //去掉该项目中一些num 为0的职位
            {
                if($name == $v['num']) unset($arr[$k]);
            }
            if(isset($arr['name'])){ //如果里面添加过成员
                foreach ($arr as $k=>$v)  //讲数据中的职位和数量取出来
                {
                    $p1 = [];
                    $p1['name'] = $v['name'];
                    $p1['num']  = $v['num'];
                    $newArray[] = $p1;
                }
                foreach ($newArray as &$n){
                    foreach ($pro_user as &$p2){
                        if($p2['name'] == $n['name']){
                            $n['num']--;
                        }
                    }
                }
                $zhiweiID = '';
                foreach ($newArray as $k=>$q)
                {
                    $username = Model::factory('personCategory');
                    $username = $username->getRow(array('name'=>$q['name']));
                    $zhiweiID .=$username['id'].",";
                }
                $zhiweiID = rtrim($zhiweiID,',');
            }else{ //如果是新项目没有匹配过成员
                $zhiweiID = '2';
                $newArray ="该项目还没有匹配过成员";
            }
            $p['zhiweiID'] = $zhiweiID;
            $p['Vacancy_occupation'] = $newArray;
//            print_r($p);exit;
        }
//        print_r($pro);exit;
        $this->content = View::factory('projectUser/list');
        $this->content->pro = $pro;
    }

    public function action_addUser()
    {
//        echo '222';exit;
        $get = $_GET['id'];
        $pid = $_GET['pid'];
//        print_r($pid);exit;

        if (!empty($_POST)) {   //选择了人之后 将这个人的信息摘出来  然后将价钱补上之后放入project_user表中
            $id =$_POST['parent_id'];
            $id = explode(',',$id);
            $uid = $id[0];//获取用户ID
            $cid = $id[1];//获取职业ID
            $model = Model::factory('projectProps');
            $model1 = $model->getRow(array('project_id'=>$pid));
//            print_r($model1);exit;
            $user = Model::factory('projectUser');
            $users= $user->getRow(array('project_id'=>$pid));
//            print_r($users);exit;
            $project_budget = $model1['project_budget'];
            $arr = json_decode($model1['prop_middle'],true);
            foreach ($arr as $k=>$v)
            {
                if( $v['num'] == 0) unset($arr[$k]);
            }
            $array = [];  //要插入的人的信息数组
           foreach($arr as $a) //循环找出这个职业对应的多少钱
           {
               if($cid == $a['cid']){
                   if($a['type'] == 2){
                       $price = $project_budget*$a['cost'];
                       $remuneration =$price;
                   }else{
                       $remuneration = $a['cost'];
                   }
                   $array['remuneration'] = $remuneration;
               }
           }
           $users['user_id'] = $uid;
           $users['person_cate_id'] = $cid;
           $users['remuneration'] = $array['remuneration'];
           $users['real_remuneration'] = 0;
           $users['accept_time'] = time();
           $users['create_time'] = time();
           $users['expire_time'] = time();
           unset($users['id']);
          $ret  = $user->insert($users);
            if ($ret !== false) {
                $where = array('status'=>2);
                $presonInfo1 = Model::factory('personInfo');
                $model = $presonInfo1->getRow(array('uid'=>$uid));
                $model1= $presonInfo1->updateById($where,$model['id']); //插入成功后修改人才的档期为无档期
                $this->redirect('projectUser/list');
            }
        }
        $id = explode(',',$get);
//        print_r($id);exit;
        $pro = Model::factory('project');
        $city = $pro->getRow(array('id'=>$pid));
        $city = $city['city'];
        foreach ($id as &$i){
            $username = Model::factory('personCategory');
            $username = $username->getRow(array('id'=>$i));
            $i = $username;
            $where = array(
                'status'=>1,
                'cid'=>array('FIND_IN_SET'=>$i['id']),
                'city'=>array( 'like'=>"%$city%"),
            );
            $presonInfo = Model::factory('personInfo');
            $presonInfo = $presonInfo->getAll($where)->as_array();
            $i['zhiwei_user'] = $presonInfo;
        }
//        print_r($id);exit;
        $this->content = View::factory('projectUser/add');
        $this->content->pro = $id;

    }
}